/**
 * file
 */
package com.tdkj.action.backstage.website;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanInformationSection;

import freemarker.template.utility.StringUtil;

/**
 * 
 * FileName: InformationSectionAction.java
 * 
 * @Description: TODO信息栏目管理
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-9-13 下午07:29:03
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-9-13 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class InformationSectionAction extends BaseAction {
	private static final long serialVersionUID = 2581427953251902729L;
	private String optionId;
	private BeanInformationSection informationSection;
	private List<BeanInformationSection> beanInformationSections;

	public String viewList() {
		Criteria criteria = this.createCriteria(BeanInformationSection.class);
		criteria.addOrder(Order.asc("subscript"));
		beanInformationSections = this.getBaseService().findBy(criteria, 1, 11).getList();
		return "viewList";
	}

	public String informationSectionEdit() {
		if (StringUtils.isNotEmpty(optionId)) {
			informationSection = this.getBaseService().findById(Long.parseLong(optionId), BeanInformationSection.class);
		}
		return "edit";
	}

	public String informationSectionSave() {
		informationSection.setId(Long.parseLong(optionId));
		boolean tag = true;
		if (null==informationSection.getSubscript()) {
			this.addFieldError("informationSection.subscript", "显示顺序不能为空");
			tag = false;
		}else if(informationSection.getSubscript()>11){
			this.addFieldError("informationSection.subscript", "格式不正确，请填写1-11数字");
			tag = false;
		}
		
		if (StringUtils.isEmpty(informationSection.getTitle())) {
			this.addFieldError("informationSection.title", "显示名称不能为空");
			tag = false;
		}
		if (StringUtils.isEmpty(informationSection.getDefaultUrl())) {
			this.addFieldError("informationSection.defaultUrl", "栏目链接不能为空");
			tag = false;
		}
		if (StringUtils.isEmpty(informationSection.getImageUrl())) {
			this.addFieldError("informationSection.imageUrl", "图标地址不能为空");
			tag = false;
		}
		if (!tag) {
			return "edit";
		}

		if (null != informationSection && StringUtils.isNotEmpty(informationSection.getId() + "")) {
			this.getBaseService().saveOrUpdate(informationSection, BeanInformationSection.class);
		}
		return viewList();
	}

	/**
	 * @return the optionId
	 */
	public String getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(String optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the informationSection
	 */
	public BeanInformationSection getInformationSection() {
		return informationSection;
	}

	/**
	 * @param informationSection
	 *            the informationSection to set
	 */
	public void setInformationSection(BeanInformationSection informationSection) {
		this.informationSection = informationSection;
	}

	/**
	 * @return the beanInformationSections
	 */
	public List<BeanInformationSection> getBeanInformationSections() {
		return beanInformationSections;
	}

	/**
	 * @param beanInformationSections
	 *            the beanInformationSections to set
	 */
	public void setBeanInformationSections(List<BeanInformationSection> beanInformationSections) {
		this.beanInformationSections = beanInformationSections;
	}

}
